﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace HelperPrototype.TabPageControls
{
    public partial class LapPhieuThanhToan : UserControl
    {
        public LapPhieuThanhToan()
        {
            InitializeComponent();
        }

        private void LapPhieuThanhToan_Load(object sender, EventArgs e)
        {
            loadData();
        }

        private void loadData()
        {
            _DB.DataClasses1DataContext dc = new _DB.DataClasses1DataContext();
            var listPhieuThanhToan = from qt in dc.QuyTrinhMuaHangs
                                     where qt.deleted == false &&
                                           qt.PhieuThanhToan != null
                                     orderby qt.PhieuThanhToan.NgayTao descending
                                     select qt.PhieuThanhToan;

            if (!Program.NguoiDungHienTai.coQuyen(40))
                listPhieuThanhToan = listPhieuThanhToan.Where(x => x.MaNguoiTao == Program.MaNguoiDungHienTai);

            dgvPhieuThanhToan.Rows.Clear();
            int count = 1;
            foreach (_DB.PhieuThanhToan p in listPhieuThanhToan)
            {
                _DB.QuyTrinhMuaHang qt = p.QuyTrinhMuaHangs.Single();
                float tienThanhToan = qt.PhieuMuaHang.getTongTien();
                if (qt.PhieuTamUng != null)
                    tienThanhToan -= qt.PhieuTamUng.SoTien;

                DataGridViewRow r = new DataGridViewRow();
                r.CreateCells(dgvPhieuThanhToan);
                r.Cells[0].Value = count++;
                r.Cells[1].Value = p.MaPhieu;
                r.Cells[2].Value = p.NguoiTao.Ten;
                r.Cells[3].Value = p.NgayTao;
                r.Cells[4].Value = (tienThanhToan > 0) ? "Phải trả  " + tienThanhToan.ToString() : "Phải thu  " + (-tienThanhToan).ToString();
                r.Cells[5].Value = qt.TrangThaiQuyTrinhMuaHang.MoTa;
                r.Tag = qt.TrangThai;

                if (qt.TrangThai == _DB.TrangThaiQuyTrinhMuaHang.KHONG_DUYET_PHIEU_THANH_TOAN)
                {
                    r.Cells[5].Value += " (" + p.LyDoKhongDuyet + ")";
                }

                dgvPhieuThanhToan.Rows.Add(r);
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            OtherForms.ThemPhieuThanhToan f = new OtherForms.ThemPhieuThanhToan();
            f.ShowDialog();
            loadData();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            // check
            if (dgvPhieuThanhToan.SelectedRows.Count <= 0)
                return;

            // check trang thai
            int trangthai = (int)dgvPhieuThanhToan.SelectedRows[0].Tag;
            if (trangthai != _DB.TrangThaiQuyTrinhMuaHang.CHINH_SUA_PHIEU_THANH_TOAN &&
                trangthai != _DB.TrangThaiQuyTrinhMuaHang.KHONG_DUYET_PHIEU_THANH_TOAN)
            {
                return;
            }

            // confirm
            if (MessageBox.Show("Chắn chắn?", "", MessageBoxButtons.YesNo) == DialogResult.No)
                return;

            // update
            string maPhieu = (string)dgvPhieuThanhToan.SelectedRows[0].Cells[1].Value;
            _DB.DataClasses1DataContext dc = new _DB.DataClasses1DataContext();
            _DB.QuyTrinhMuaHang qt = dc.QuyTrinhMuaHangs.Single(x => x.MaPhieuThanhToan == maPhieu);
            qt.TrangThai = _DB.TrangThaiQuyTrinhMuaHang.CHO_DUYET_PHIEU_THANH_TOAN;
            dc.SubmitChanges();
            loadData();
        }

        private void dgvPhieuThanhToan_DoubleClick(object sender, EventArgs e)
        {
            if (dgvPhieuThanhToan.SelectedRows.Count > 0)
            {
                string maPhieu = (string)dgvPhieuThanhToan.SelectedRows[0].Cells[1].Value;
                OtherForms.XemPhieuThanhToan f = new OtherForms.XemPhieuThanhToan(maPhieu);
                f.Show();
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // check
            if (dgvPhieuThanhToan.SelectedRows.Count <= 0)
                return;

            // check trang thai
            int trangthai = (int)dgvPhieuThanhToan.SelectedRows[0].Tag;
            if (trangthai != _DB.TrangThaiQuyTrinhMuaHang.CHINH_SUA_PHIEU_THANH_TOAN &&
                trangthai != _DB.TrangThaiQuyTrinhMuaHang.KHONG_DUYET_PHIEU_THANH_TOAN)
            {
                return;
            }

            // go
            string maPhieu = (string)dgvPhieuThanhToan.SelectedRows[0].Cells[1].Value;
            OtherForms.ThemPhieuThanhToan f = new OtherForms.ThemPhieuThanhToan(maPhieu);
            f.ShowDialog();
            loadData();
        }

        private void button5_Click(object sender, EventArgs e)
        {
            // check
            if (dgvPhieuThanhToan.SelectedRows.Count <= 0)
                return;

            // check trang thai
            int trangthai = (int)dgvPhieuThanhToan.SelectedRows[0].Tag;
            if (trangthai != _DB.TrangThaiQuyTrinhMuaHang.CHINH_SUA_PHIEU_THANH_TOAN &&
                trangthai != _DB.TrangThaiQuyTrinhMuaHang.KHONG_DUYET_PHIEU_THANH_TOAN)
            {
                return;
            }

            // confirm
            if (MessageBox.Show("Chắn chắn?", "", MessageBoxButtons.YesNo) == DialogResult.No)
                return;

            // go
            string maPhieu = (string)dgvPhieuThanhToan.SelectedRows[0].Cells[1].Value;
            _DB.DataClasses1DataContext dc = new _DB.DataClasses1DataContext();
            _DB.QuyTrinhMuaHang qt = dc.QuyTrinhMuaHangs.Single(x => x.MaPhieuThanhToan == maPhieu);
            _DB.PhieuThanhToan p = dc.PhieuThanhToans.Single(x => x.MaPhieu == maPhieu);
            p.deleted = true;
            qt.MaPhieuThanhToan = null;
            qt.TrangThai = _DB.TrangThaiQuyTrinhMuaHang.DA_NHAP_KHO;
            dc.SubmitChanges();
            loadData();
        }


    }
}
